<template>
	<view class="container">
		<view class="left-top-sign">LOGIN</view>
		<view class="welcome">欢迎回来！</view>
		<button class="confirm-btn" open-type="getUserInfo" @getuserinfo="wxLogin" :disabled="logining">微信授权登录</button>
		<button class="confirm-btn" open-type="getPhoneNumber" @getphonenumber="getPhoneNumber">获取手机号</button>
		<button class="confirm-btn" @click="scanCode">扫码</button>
		<button class="confirm-btn" @click="scanCode">扫码</button>
	</view>
</template>

<script>
	import Storage from '@/common/scripts/storage.js'
	import config from '@/common/scripts/config.js'
	import IndexService from './service/service.js'
	import WXBizDataCrypt from '@/static/scripts/WXBizDataCrypt.js'

	export default {
		beforeCreate() {
			this.service = new IndexService()
		},
		name: 'index-login',
		data() {
			return {
				logining: false,
				appid: config.appid,
				secret: config.secret,
				openURL: config.openURL,
				provider: config.provider,
				sessionKey: null
			}
		},
		onLoad(options) {
			Storage.set('back-page', options)
		},
		methods: {
			scanCode() {
				uni.scanCode({
					success: function(res) {
						console.log(JSON.stringify(res))
					}
				})
			},
			toIndex() {
				uni.redirectTo({
					url: '/pages/index/index'
				})
			},
			init() {
				// #ifdef MP-WEIXIN
				this.initWx()
				// #endif
			},
			initWx() {
				let that = this
				that.service.login({})
					.then(that.loginSuccess).catch(that.loginError)
					.then(that.getOpenIDSuccess).catch(that.getOpenIDError)
					.then(that.getAccessTokenSuccess).catch(that.getAccessTokenError)
					.then(that.getUnionIDSuccess).catch(that.getUnionIDError)
			},
			getUnionIDSuccess(result) {
				if (result) {

				}
			},
			getUnionIDError(err) {
				if (err) {
					return null
				}
			},
			getAccessTokenSuccess(result) {
				let that = this
				if (result) {
					return this.service.getUnionID({
						access_token: result.access_token,
						openid: result.openid
					})
				}
			},
			getAccessTokenError(err) {
				if (err) {
					return null
				}
			},
			loginSuccess(result) {
				if (result) {
					return this.service.getOpenID({
						code: result.code
					})
				} else {
					return null
				}
			},
			loginError(err) {
				if (err) {
					return null
				}
			},
			getOpenIDSuccess(result) {
				console.log(result)
				if (result) {
					this.sessionKey = result.session_key
					return this.service.getAccessToken(result)
				}
			},
			getOpenIDError(err) {
				console.log(err)
				if (err) {
					return null
				}
			},
			getPhoneNumber(e) { //这个事件同样需要拿到e
				let that = this
				let ency = e.detail.encryptedData;
				let iv = e.detail.iv;
				let errMsg = e.detail.errMsg

				var pc = new WXBizDataCrypt(config.appid, that.sessionKey)

				var data = pc.decryptData(ency, iv)

				console.log('解密后 data: ', data)
			},
			wxLogin(e) {
				let that = this;
				that.logining = true;
				let userInfo = e.detail.userInfo
				uni.login({
					provider: that.provider,
					success: (login_res => {
						let code = login_res.code
						uni.getUserInfo({
							success(info_res) {
								uni.hideLoading()
								uni.navigateBack()
							}
						})
					})
				})
			}
		},
		created() {
			this.init()
		}
	}
</script>

<style lang="scss">
	.container {
		display: flex;
		overflow: hidden;
		background: #fff;
		flex-direction: column;
		justify-content: center;

		.left-top-sign {
			font-size: 120upx;
			color: #000000;
			position: relative;
			left: -10upx;
			margin-top: 100upx;
		}

		.welcome {
			position: relative;
			left: 50upx;
			top: -90upx;
			font-size: 46upx;
			color: #555;
			text-shadow: 1px 0px 1px rgba(0, 0, 0, 0.3);
		}

		.confirm-btn {
			width: 630upx;
			height: 76upx;
			line-height: 76upx;
			border-radius: 50px;
			margin-top: 70upx;
			background: #000000;
			color: #fff;
			font-size: 16upx;

			&:after {
				border-radius: 100px;
			}
		}
	}
</style>
